/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.vasilrem.linked

import scala.actors._
import scala.actors.Actor._

object LinkedActors {

  case class Die(reason:AnyRef)
  case class Divide(number: Int)

  def status(name: String, actor: Actor) = println("Actor %s is %s" format(name, actor.getState))
  def printMessage(actorName: String):PartialFunction[Any, Unit] = {case msg => println("Actor %s received %s" format(actorName, msg))}

  def start(isTrapExit: Boolean, message: Any) = {

    val A = actor{
      self.trapExit = true
      loop(react(printMessage("a")))
    }

    val B = actor{
      self.trapExit = isTrapExit
      self link A
      loop(react(printMessage("b")))
    }
    
    val C = actor{
      self link B
      loop{
        react{
          case Die(reason) => exit(reason)
          case Divide(number) => 1/number
        }
      }
    }

    C! message
    
    Thread.sleep(1000)

    status("b", B)
    status("c", C)
  }

}
